/* ==========================================================================
 常用函数封装
 ========================================================================== */
/**
 * tab切换
 */
function tab(tit,list,className,way,num){
    var titLi = $(tit).children().eq(0).children();
    var listDiv = $(list).children();
    listDiv.eq(num).siblings().hide();
    titLi.eq(num).addClass(className);
    titLi.on(way,function(){
        $(this).addClass(className).siblings().removeClass(className);
        listDiv.eq($(this).index()).show().siblings().hide();
    });
}

/**
* banner点击切换效果
*/
function bannerBase(bannerDiv){
    var imgUl = $(bannerDiv).children().eq(0);  
    var imgLi = imgUl.children();
    var prev = $(bannerDiv).children().eq(1);
    var next = $(bannerDiv).children().eq(2); 
    var W = $(bannerDiv).parent().width();
    var len = imgLi.size();
    prev.addClass("noClick");
    //图片ul的宽度
    imgUl.width(len*W);
    //封装轮播动画
    var i=0; 
    function move(){
        imgUl.stop().animate({left:-i*W},1000);
    }       
    //鼠标经过--左右按钮
    $(bannerDiv).hover(function(){    
        prev.fadeToggle();
        next.fadeToggle();
    });   
    //点击翻页按钮实现图片切换
    prev.click(function(){  
        next.removeClass("noClick");
        i--;
        if(i==0){
            prev.addClass("noClick");
        }
        if(i==-1){
            i=0;     
        }
        move();
    });
    next.click(function(){
        prev.removeClass("noClick");
        i++;
        if(i==len-1){
            next.addClass("noClick");
        }
        if(i==len){
            i=len-1;
        }
        move();
    });
}

/**
 * banner左右切换效果
 */
function bannerCommon(bannerDiv,className){
    //无缝轮播，克隆第一张图片
    $(bannerDiv).children().eq(0).children().eq(0).clone().appendTo($(bannerDiv).children().eq(0));

    var imgUl = $(bannerDiv).children().eq(0);
    var imgLi = imgUl.children();
    var btnLi = $(bannerDiv).children().eq(1).children();
    var prev = $(bannerDiv).children().eq(2);
    var next = $(bannerDiv).children().eq(3);

    btnLi.eq(0).addClass(className);
    var W = $(bannerDiv).width();
    var len = imgLi.size();
    //图片ul的宽度
    imgUl.width(len*W);
    //封装轮播动画
    var i=0,t;
    function move(){
        if(i == len){
            imgUl.css({left:"0"});
            i = 1;
        }
        if(i == -1){
            imgUl.css({left:-(len-1)*W});
            i = len-2;
        }
        imgUl.stop().animate({left:-i*W},1000);
        //当前分页按钮颜色变化
        setTimeout(function(){
            if(i == len-1){
                btnLi.eq(0).addClass(className).siblings().removeClass(className);
            }else{
                btnLi.eq(i).addClass(className).siblings().removeClass(className);
            }
        },300);
    }
    t = setInterval(function(){
        i++;
        move();
    },3000);
    //鼠标经过停止动画
    $(bannerDiv).hover(
        function(){
            clearInterval(t);
            prev.fadeIn();
            next.fadeIn();
        },
        function(){
            t = setInterval(function(){
                i++;
                move();
            },3000);
            prev.fadeOut();
            next.fadeOut();
        });
    //进入分页按钮实现图片切换
    btnLi.mouseenter(function(){
        var index = $(this).index();
        imgUl.stop().animate({left:-index*W},1000);
        i = index;
        $(this).addClass(className).siblings().removeClass(className);
    });
    //点击翻页按钮实现图片切换
    prev.click(function(){
        i--;
        move();
    });
    next.click(function(){
        i++;
        move();
    });
}

/**
 * 淡入淡出banner效果
 */
function bannerFade(bannerDiv,className){
    var imgLi = $(bannerDiv).children().eq(0).children();
    var btnLi = $(bannerDiv).children().eq(1).children();
    var len = imgLi.size();
    var i = 0;
    imgLi.css({display:"none"});
    imgLi.eq(0).css({display:"block"});
    btnLi.eq(0).addClass(className);

    function ani(){
        i++;
        if(i == len){
            imgLi.eq(0).fadeIn(500);
            imgLi.eq(len-1).fadeOut(500);
            btnLi.eq(0).addClass(className).siblings().removeClass(className);
            i = 0;
        }else{
            imgLi.eq(i).fadeIn(500);
            imgLi.eq(i-1).fadeOut(500);
            btnLi.eq(i).addClass(className).siblings().removeClass(className);
        }
    }

    var t =setInterval(ani,3000);

    $(bannerDiv).hover(function(){
        clearInterval(t);
    },function(){
        t =setInterval(ani,3000);
    });

    btnLi.mouseenter(function(){
        var cur = $(this).index();
        i = cur;
        $(imgLi).eq(cur).fadeIn(500).siblings().fadeOut(500);
        $(this).addClass(className).siblings().removeClass(className);
    });
}

/*
手风琴效果
 */
function slideQin(boxDiv,className,num){
    var boxLi = $(boxDiv).children().eq(0).children();
    boxLi.eq(num).children().eq(0).addClass(className);
    boxLi.eq(num).children().eq(1).show();
    boxLi.children("a").on("mouseenter",function(event){
        $(this).addClass(className);
        $(this).next().slideDown(200);
        $(this).parent().siblings().children("a").removeClass(className).next().hide();
    });
}

/**
* 时间倒计时
*/
var countDown = function(time){
    var now = new Date();
    var y = now.getFullYear();  //四位数字年份
    var m = now.getMonth()+1;  //1到12月
    var d = now.getDate()+1;  //1到31加1
    var num1 = (m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&(d==32);
    var num2 = (m==2||m==4||m==6||m==9||m==11)&&(d==31);
    if(num1 || num2){
        now.setDate(d);
        var d = now.getDate();
        m++;
        if(m>12){
            m-=12;
            y++;
        }
    }
    str = y + "-" + m + "-" + d + " " + time;
    var spec = new Date(str);    
    //兼容IE
    if(isNaN(spec)){   
        spec =spec.replace(/-/,"/");
    }
    return function(){
        var now = new Date();
        var h = now.getHours();
        var m = now.getMinutes();
        var s = now.getSeconds();
        var endTime = new Date(spec);
        var milliInterval = endTime - now ;  
        var milli = parseInt(milliInterval%1000/100);  
        var seconds = parseInt(milliInterval/1000) ; 
        var secondsEnd = seconds % 60 ; 
        var minutes = (seconds - secondsEnd)/60;   
        var minutesEnd = minutes % 60; 
        var hours = ( minutes - minutesEnd )/60;  
        var hoursEnd = hours % 24;
        if(hoursEnd<10){
            hoursEnd = "0" + hoursEnd;            
        }
        if(minutesEnd<10){
            minutesEnd = "0" + minutesEnd;
        }
        if(secondsEnd<10){
            secondsEnd = "0" + secondsEnd;
        }
        return{
            hoursEnd:hoursEnd,
            minutesEnd:minutesEnd,
            secondsEnd:secondsEnd,
            h:h,
            m:m,
            s:s
        }
    }    
}

/**
 * 延迟加载
 */
function lazyLoad(){

}

/**
 * 登录注册部分
 */
//手机与密码验证
function validate(value,hidden,expression){
    $(value).click(function(){
        $(hidden).show();
        $(value).blur(function(){
            var str = $(value).val();
            var pattern = new RegExp(expression);
            var result = pattern.test(str);
            if(result){
                $(hidden).hide();
            }else{
                $(hidden).show();
            }
        });   
    });
}

/* ==========================================================================
 网页头尾公用
 ========================================================================== */
/**
 * top 部分
 */
function toper(){
    $(".top .topLeft,.top .topRight").children("li.down").hover(function(){
        $(this).children().eq(1).css({display:"block"});
    },
    function(){
        $(this).children().eq(1).css({display:"none"});
    });

    //切换地点
    var itemA = $(".topLeft .areaLayer").children("div.item").children("a");
    itemA.eq(0).addClass("bg");
    itemA.click(function(){
        $(this).addClass("bg").parent().siblings().children().removeClass("bg");
        $(".topLeft").find("span").text($(this).text());
    });
}
toper();

/**
* header 部分
*/
function header(){
    $(".car").hover(function(){
        $(this).children(".carLayer").css({display:"block"});
    },
    function(){
        $(this).children(".carLayer").css({display:"none"});
    });
}
header();

/**
 * 下拉列表 部分
 */
function listMenu(){
    $(".listMenu").children().eq(0).children().hover(function(){
        $(this).addClass("white");
        $(this).children().eq(1).css({display:"block"});
    },function(){
        $(this).removeClass("white");
        $(this).children().eq(1).css({display:"none"});
    });
    $(".all").on("click",function(){
        $(this).find(".listMenu").slideToggle();
    });
}
listMenu();

/**
 * 电梯部分
 */
function elevator(){
    var size=$(".leftSlider li").size();
    $(".leftSlider li").click(function(){
        var index = $(this).index();
        var T = $(".d"+index).offset().top - 200;
        $("body").animate({scrollTop:T});
    });
    $(window).scroll(function(){         
        var T=$(window).scrollTop();
        for(var j=0; j<size; j++){
            var t = $(".d"+j).offset().top - 400;
             if(T>=t){
             $(".leftSlider li").eq(j).addClass(".leftSlider focus").siblings().removeClass(".leftSlider focus");
          }
        }

    });
}
elevator();

/**
* 侧边栏部分
*/
function collect(){
    function createCode (){
        var str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var len = str.length; //50
        var index,chr;
        for (var i = 0; i < 4; i++){
            index = Math.floor(Math.random()*len);
            chr = str.charAt(index);
            $(".authCodes .verify").append(chr);
        }          
    }
    createCode();
    function show(){
        $(".chang").on("click",function(){
            $(".authCodes .verify").text("");
            createCode();
        });
    }
    show();
    function show1(a,b){
        var timer;
            $(a).mouseenter(function(){
                $(b).show();
            });
            $(a).mouseleave(function(){
                 timer = setTimeout(function(){
                    $(b).hide();
                },500);
            });
            $(b).mouseenter(function(){
                    clearTimeout(timer);
                    $(this).show();
                }).mouseleave(function(){
                    $(this).hide();
                });
    }
    show1(".account i",".account .block");
    show1(".collect .qrCode",".qrCode i");
    show1(".collect .service i",".blocks ");

    //会员中心部分
    $(".collect .accommodate .vip .main").height($(window).height()-$(".collect .accommodate .vipHeader").outerHeight());

    // 购物车部分
    $(".collect .accommodate .car .main").height($(window).height()-$(".collect .accommodate .carHeader").outerHeight()-$(".collect .accommodate .carFooter").outerHeight());

    // 收藏商品部分
    $(".collect .accommodate .attention .main").height($(window).height()-$(".collect .accommodate .attHeader").outerHeight());
    $(".collect .accommodate .attention .main .tabNav").find("li").on("mouseenter",function(){
        $(this).addClass("current").siblings().removeClass("current");
        var m = $(this).index();
        $(".collect .accommodate .attention .main").children(".content").animate({
            "left":-270*m
        },500);
    });

    // 侧边栏打开与切换
    var flag = 0;
    if(flag == 0){
        $(".collect .account").on("click",function(){
            if($(".collect .vip").css("left") == "270px"){
                $(this).parent().animate({"right":270},400);
                $(this).parent().find(".vip").animate({"left":0},400);
                $(this).parent().find(".car").animate({"left":270},400);
                $(this).parent().find(".attention").animate({"left":270},400);
                flag = 1;
                return false;
            }else{
                $(this).parent().animate({"right":0},400);
                $(this).parent().find(".vip").animate({"left":270},400);
            }
    });
        $(".collect .shoppingCart").on("click",function(){
            if($(".collect .car").css("left") == "270px"){
                $(this).parent().animate({"right":270},400);
                $(this).parent().find(".car").animate({"left":0},400);
                $(this).parent().find(".vip").animate({"left":270},400);
                $(this).parent().find(".attention").animate({"left":270},400);
                flag = 1;
                return false;
            }else{
                $(this).parent().animate({"right":0},400);
                $(this).parent().find(".car").animate({"left":270},400);
            }
        });
        $(".collect .enshrine").on("click",function(){
            if($(".collect .attention").css("left") == "270px"){
                $(this).parent().animate({"right":270},400);
                $(this).parent().find(".attention").animate({"left":0},400);
                $(this).parent().find(".vip").animate({"left":270},400);
                $(this).parent().find(".car").animate({"left":270},400);
                flag = 1;
                return false;
            }else{
                $(this).parent().animate({"right":0},400);
                $(this).parent().find(".attention").animate({"left":270},400);
            }
        });
    }

    $(".collect .accommodate .close").on("click",function(){
        $(".collect").animate({"right":0},400);
        flag=0;
    });

    $(window).on("click", function(){
        $(".collect").animate({"right":0},400);
        flag=0;
    });


    /**
     * 返回顶部
     */
    function toTop(){
        $(".collect .toTop").click(function(){
            $("html,body").animate({
                scrollTop:0
            },500)
        });
    }
    toTop();
}
collect();
